package 华为OD机试真题2023.基础题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HJ31_优秀学员统计 {
  public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/31.txt"));
    int N = in.nextInt();
    List<Yuangong> result = new ArrayList<>();
    for (int i = 0; i < N; i++) {
      result.add(new Yuangong(i, 0, -1));
    }
    int[] cnt = new int[30];
    for (int i = 0; i < 30; i++) {
      cnt[i] = in.nextInt();
    }
    for (int i = 0; i < 30; i++) {
      while (cnt[i]-- > 0) {
        int no = in.nextInt();
        Yuangong now = result.get(no);
        now.count++;
        now.start = now.start == -1 ? i : now.start;
      }
    }
    result.sort(
        (a, b) -> {
          if (b.count == a.count) {
            if (a.start == b.start) {
              return a.no - b.no;
            } else {
              return a.start - b.start;
            }
          } else {
            return b.count - a.count;
          }
        });
    int M = Math.min(N, 5);
    for (int i = 0; i < M; i++) {
      System.out.print(result.get(i).no + " ");
    }
  }

  static class Yuangong {
    int no;
    int count;
    int start;

    public Yuangong(int no, int count, int start) {
      this.no = no;
      this.count = count;
      this.start = start;
    }
  }
}
